package cn.hxzy.course.controller;


import cn.hxzy.common.domain.*;

import cn.hxzy.common.domain.vo.ChpSecListVo;
import cn.hxzy.common.domain.vo.CourseListVo;
import cn.hxzy.common.domain.vo.CourseVoList;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.course.service.*;
import cn.hxzy.common.util.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

// 假设CourseService是处理业务逻辑的服务类
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private DirectionService directionService;
    @Autowired
    private ProfessionalService professionalService;

    @Autowired
    private ChapterService chapterService;

    @GetMapping("/getCourseList/{courseId}")
    public R<List<CourseVoList>> getCourseList(@PathVariable Integer courseId) {
        List<CourseVoList> courseList = courseService.getList(courseId);
        return R.success(courseList);
    }
    @GetMapping("/getCourseListUserId")
    public R<List<CourseVoList>> getCourseListUserId() {
        List<CourseVoList> courseList = courseService.getUserIdList();
        return R.success(courseList);
    }



    @GetMapping("/selectCourceListByName/{courseName}")
    public R<List<CourseVoList>> selectCourceListByName(@PathVariable String courseName){
        List<CourseVoList> courseVoLists = courseService.selectCourceListByName(courseName);
        return R.success(courseVoLists);
    }
    @GetMapping("/selectListDirectionId/{directionId}")
    public R<List<CourseVoList>> selectListDirectionId(@PathVariable String  directionId){
        List<CourseVoList> courseVoLists = courseService.selectListDirectionId(directionId);
        return R.success(courseVoLists);

    }



    /**
     * 专业课程名称列表
     */
    @GetMapping("/listProfessional")
    public R<List<Professional>> listProfessional() {
        List<Professional> professionalList = professionalService.list();
        if (professionalList != null) {
            return R.success(professionalList);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }
    /**
     * 专业方向名称列表
     * @return
     */
    @GetMapping("listDirection")
    public R<List<Direction>> listDirection() {
        List<Direction> directions = directionService.list();
        if (directions != null) {
            return R.success(directions);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    /**
     * 添加课程
     *
     * @param course 课程信息
     * @return
     */
    @PostMapping("/save")
    public R saveCourse(@RequestBody Course course) {
        User user = ThreadLocalUtil.getUser();
        Long userId = user.getUserId();
        String userType = user.getUserType();
        List<String> courseLists = courseService.getCourseLists(userId.intValue());
        for (int j = 0; j < courseLists.size(); j++) {
            if (course.getCourseName().equals(courseLists.get(j))){
                return R.error(411,"该课程已存在!");
            }
        }
        if (userType.equals("1")){
            Course course1=new Course();
            course1.setSchoolId(user.getSchoolId());
            if (course.getCourseName()!=null && course.getCourseName().equals("")){
                return R.error(411,"课程名称不能为空!");
            }
            course1.setCourseName(course.getCourseName());
            course1.setCourseId(course.getCourseId());
            course1.setCreateUser(userId.intValue());
            course1.setCourseInfo(course.getCourseInfo());
            course1.setProfessionalId(course.getProfessionalId());
            course1.setDirectionId(course.getDirectionId());
            course1.setLessonHours(course.getLessonHours());
            course1.setCreateTime(LocalDateTime.now());
            course1.setUpdateTime(LocalDateTime.now());
            int i = courseService.saveCourse(course1);
            if (i == 0) {
                return R.error(410, "新增失败!");
            } else {
                return R.success("新增成功!");
            }
        }else {
            return R.error(451,"您没有权限创建课程哦!");
        }
        }
@GetMapping("/getList")
public R<List<Course>> a() {
    List<Course> courses = courseService.selectAll();
    return R.success(courses);
}

    /**
     * 获取课程列表
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<CourseListVo>> geList() {
        List<CourseListVo> courseList = courseService.getCourseList();
        return R.success(courseList);
    }
    //根据专业方向查课程列表
    @GetMapping("/getCourseListByDirectionId")
    public R<List<CourseListVo>> getCourseListByDirectionId(
            @RequestParam(required = false) Integer directionId,
            @RequestParam(required = false) Integer professionalId,
            @RequestParam(required = false)String courseName
    ) {

        List<CourseListVo> courseList = courseService.getCourseListByDirectionId(directionId,professionalId,courseName);
        return R.success(courseList);

    }
    //根据课程名称模糊查询
    @GetMapping("/getCourseListByName")
    public R<List<CourseListVo>> getCourseListByName(@RequestParam String courseName) {
        List<CourseListVo> courseListByName = courseService.getCourseListByName(courseName);
        return R.success(courseListByName);
    }

    /**
     * 根据课程ID查询所有的章节
     *
     * @return
     */
    @GetMapping("/select")
    public R<List<Chapter>> courseList(@RequestParam(required = false) Integer courseId) {
        List<Chapter> chapters = chapterService.courseList(courseId);
        if (!chapters.isEmpty()) {
            return R.success(chapters);
        }
        return null;
    }

    /**
     * 创建章节(某个课程下面的章节)
     *
     * @return
     */

    @PostMapping("/increaseChapter")
    public R increaseChapter(@RequestBody Chapter chapter) {
        int i = chapterService.increaseChapter(chapter);
        if (i > 0) {
            return R.success("新增成功");
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 根据章节ID修改某章节名称
     *
     * @param chapterId
     * @return
     */
    @PutMapping("/update")
    public R updateChapter(@RequestParam Integer chapterId, @RequestParam String chapterName) {
        int i = chapterService.updateChapter(chapterId, chapterName);
        if (i > 0) {
            return R.success("修改成功");

        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 根据章节ID删除该章节
     *
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteById(@RequestParam Integer chapterId) {
        boolean b = chapterService.removeById(chapterId);
        if (b) {
            return R.success("删除成功");
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 根据课程编号获取课程信息
     *
     * @param courseId 课程编号
     * @return
     */
    @GetMapping("/getCourse/{courseId}")
    public R<Course> getCourseById(@PathVariable int courseId) {
        Course course = courseService.getCourseById(courseId);
        if (course != null) {
            return R.success(course);
        }
        return R.error(402, "无数据!");
    }


    //获取当前课程的章节小节列表
    @GetMapping("/chpSecList")
    public R<List<ChpSecListVo>> getChpSecList(@RequestParam Integer courseId) {
        List<ChpSecListVo> chpSecList = courseService.ChpSecListVo(courseId);
        return R.success(chpSecList);
    }

    //删除章节以及小节
    @DeleteMapping("/deleteChapter")
    public R deleteChapter(@RequestParam Integer chapterId) {
        int i = courseService.deleteChapter(chapterId);
        if (i == 0) {
            return R.error(410, "删除失败!");
        } else {
            return R.success("删除成功!");
        }
    }

    //删除课程
    @DeleteMapping("/deleteCourse")
    public R deleteCourse(@RequestParam Integer courseId) {
        int i = courseService.deleteCourse(courseId);
        if (i == 1) {
            return R.success(101);
        } else if (i == 0) {
            return R.success(102);
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    //修改课程图标
    @PostMapping("/updateCourse")
    public R<Integer>  updatePic(@RequestBody Map map) {
        boolean update = courseService.update(new UpdateWrapper<Course>().
                eq("course_id", map.get("courseId"))
                .set("image", map.get("userAvatar"))
        );

        if (update==true){
            return R.success(1);
        }
        return R.error(505,"修改失败");
    }
    @PostMapping("/selectCourse/{courseId}")
    public R<String>  updatePic11(@PathVariable Integer courseId) {
        String s = courseService.selectAll00(courseId);

        return R.success(s);
    }
}
